package com.example.dict.utils.Dictionary.Operations;


import com.example.dict.data.WordLoader;
import com.example.dict.utils.Dictionary.dictionaryOperations;

/**
 *
 * 字典树实现
 * @author wey
 * */
public class PATTree implements dictionaryOperations {

    /**
     * 全局字典
     * */
    static PATTree dictionary;

    /**
     * 根节点
     * */
    static TrieNode root = new TrieNode();

    /**
     *
     * 初始化
     * */
    private PATTree() {}

    /**
     * 获取字典
     * */
    public static PATTree acquire() {
        if (dictionary == null) {
            dictionary = new PATTree();

            for (char letter = 'a'; letter <= 'z'; letter++) {
                String src = "dictionary_storage/" + letter + ".txt";
                WordLoader wordLoader = new WordLoader(dictionary, src);
                wordLoader.start();
            }
        }
        return dictionary;
    }

    /**
     * 翻译单词
     *
     * @param word 单词
     * @return 翻译
     * */
    @Override
    public String translate(String word) {
        if (word == null || word.isEmpty()) {
            return null;
        }
        char[] chars = word.toCharArray();

        TrieNode current = root;

        for(char item : chars) {
            if (null == root.nextNode[item - 'a']) {
                return null;
            }

            current = current.nextNode[item - 'a'];
        }

        return current.translation;
    }


    /**
     * 储存到字典
     * @param word 单词
     * */
    @Override
    public void insert(String word, String translation) {
        if (word == null || word.isEmpty() || translation == null || translation.isEmpty()) { return ;}

        char[] chars = word.toCharArray();
        TrieNode current = root;
        for(char item : chars) {
            if (item == '.' || item == '-' || item == '!' || item == '?' || item == '*' || item == '/'||item == '\'') {
                continue;
            }

            if (null == current.nextNode[item - 'a']) {
                current.nextNode[item - 'a'] = new TrieNode();
                current.child++;
            }
            current = current.nextNode[item - 'a'];
        }

        current.translation = translation;
    }
    /**
     * 删除记录
     * @param word 单词
     * */
    @Override
    public void delete(String word) {
        if (word == null || word.isEmpty()) {
            return;
        }
        char[] chars = word.toCharArray();

        delete(0, chars, root);
    }


    /**
     *
     * 单词删除操作
     *
     * @param cur 删除了一个从第x位到x+chars.length的单词
     * @param chars 单词
     * @param root 树节点
     *
     * @return 删除该单词后的字典树
     * */
    private TrieNode delete(int cur, char[] chars, TrieNode root) {
        if (null == root || null == root.nextNode[chars[cur] - 'a']) {
            return null;
        }

        if (chars.length - 1 == cur) {
            root.nextNode[chars[cur] - 'a'].translation = null;

            if (root.nextNode[chars[cur] - 'a'].child == 0) {
                root.nextNode[chars[cur] - 'a'] = null;
                root.child--;
            }

            return root;
        }

        root = delete(cur + 1, chars, root.nextNode[chars[cur] - 'a']);

        if (root.child == 0) {
            root = null;
        }
        return root;
    }


    @Override
    public String translateC2E(String word){
        return null;
    }
    private static class TrieNode {

        /**
         * 以当前字母结尾的单词的翻译
         * */
        public String translation;

        public int child;

        public TrieNode[] nextNode = new TrieNode[128];
    }

}
